package com.joseph.websocket.client;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.http.DefaultHttpHeaders;
import io.netty.handler.codec.http.HttpClientCodec;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.websocketx.*;
import io.netty.handler.timeout.IdleStateHandler;
import org.openjdk.jol.info.ClassLayout;
import org.springframework.objenesis.Objenesis;
import org.springframework.objenesis.ObjenesisStd;
import org.springframework.objenesis.instantiator.ObjectInstantiator;
import org.springframework.util.ClassUtils;
import sun.misc.Unsafe;
import sun.reflect.Reflection;
import sun.reflect.ReflectionFactory;

import java.lang.reflect.Constructor;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

public class Client {

    private Channel channel;

    public Client(int code) {
        init(code);
    }

    public void init(final int code) {
        NioEventLoopGroup group = new NioEventLoopGroup();
        Bootstrap boot = new Bootstrap();
        boot.option(ChannelOption.SO_KEEPALIVE, true)
                .group(group)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    protected void initChannel(SocketChannel socketChannel) throws Exception {
                        ChannelPipeline p = socketChannel.pipeline();
                        p.addLast(new HttpClientCodec());
                        p.addLast(new HttpObjectAggregator(1024 * 1024 * 10));
                        p.addLast(new IdleStateHandler(5, 0, 0, TimeUnit.SECONDS));
//                        p.addLast(new ClientIdleHandler());
                        p.addLast("hookedHandler", new WebSocketClientHandler());
                    }
                });
        String host = System.getProperty("ws.url");
        String port = System.getProperty("ws.port");
        URI websocketURI = null;
        try {
            websocketURI = new URI(String.format("ws://%s:%s/ws", host, port));
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        HttpHeaders httpHeaders = new DefaultHttpHeaders();
        //进行握手
        WebSocketClientHandshaker handshaker = WebSocketClientHandshakerFactory.newHandshaker(websocketURI, WebSocketVersion.V13, (String) null, true, httpHeaders);
        try {
            channel = boot.connect(websocketURI.getHost(), websocketURI.getPort()).sync().channel();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        WebSocketClientHandler handler = (WebSocketClientHandler) channel.pipeline().get("hookedHandler");
        handler.setHandshaker(handshaker);
        handshaker.handshake(channel);
        //阻塞等待是否握手成功
        try {
            handler.handshakeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws Exception {
        //PrivateConClass privateConClass1 = PrivateConClass.class.newInstance();
        Objenesis objenesis = new ObjenesisStd(true);
        ObjectInstantiator<PrivateConClass> instantiatorOf = objenesis.getInstantiatorOf(PrivateConClass.class);
        instantiatorOf.newInstance().say();
    }
}
